<?php

namespace App\Http\Controllers\ChatGroup\Web;

use App\Models\ChatGroup\ChatGroupUserModel;
use App\Service\ChatGroup\ChatGroupHistoryService;
use App\Service\ChatGroup\ChatGroupNotifyService;
use App\Service\ChatGroup\ChatGroupSessionService;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use App\Service\ChatGroup\CustomerServeService;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class CustomerServeController extends BaseController
{
    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function chatGroupList(Request $request): JsonResponse
    {
        $userId     = getRmsUserId();
        $pageSize   = (int)$request->get('pageSize', 20);
        $pageNum    = (int)$request->get('pageNum', 1);
        $serveIn    = (int)$request->get('serveIn', -1);
        $shopIds    = $request->get('shopIds', '');
        $shopCode   = (int)$request->get('shopCode', 0);
        $shopIds    = array_filter(explode(',', $shopIds));
        $shopStatus = (string)$request->get('shopStatus', -1);
        $content    = (string)$request->get('content', '');

        $groupList = CustomerServeService::getGroupList(
            $userId,
            $pageSize,
            $pageNum,
            $shopIds,
            $shopStatus,
            $serveIn,
            $content,
            $shopCode
        );

        return $this->success($groupList);
    }

    /**
     * 进入会话
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function enterSession(Request $request)
    {
        $groupId = $request->get('groupId');
        $userId  = getRmsUserId();

        $this->validate($request, [
            'groupId' => 'required|integer',
        ]);

        return $this->success((new ChatGroupSessionService())
            ->enterSession(0, $groupId, ChatGroupSessionService::FROM_WEB, $userId));
    }

    /**
     * 聊天记录
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function getChatHistory(Request $request): JsonResponse
    {
        $groupId  = $request->get('groupId');
        $topMsgId = $request->get('topMsgId');
        $limit    = $request->get('limit', 30);
        $userId   = getRmsUserId();

        $this->validate($request, [
            'groupId'  => 'required|integer|min:1',
            'topMsgId' => 'nullable|integer',
            'limit'    => 'nullable|integer',
        ]);

        $data = ChatGroupHistoryService::getChatHistory(0, $groupId, $topMsgId, $limit, $userId);

        return $this->success($data);
    }


    /**
     * @param Request $request
     * @return void
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws GuzzleException
     */
    public function serveIn(Request $request)
    {
        $groupId = $request->get('groupId');
        $userId  = getRmsUserId();

        $this->validate($request, [
            'groupId' => 'required|integer|min:1'
        ]);

        (new ChatGroupSessionService)->serveIn($groupId, $userId);
    }


    /**
     * 转接顾问
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws Exception
     */
    public function transferConsultant(Request $request): JsonResponse
    {
        $groupId = $request->get('groupId');
        $shopId  = $request->get('shopId');
        $tag     = $request->get('tag', ChatGroupUserModel::TAG_CASHIER_CONSULTANT);

        $this->validate($request, [
            'groupId' => 'required|integer|min:1',
            'shopId'  => 'required|integer|min:1'
        ]);

        ChatGroupNotifyService::callServeCounselor($shopId, $groupId, $tag);

        return $this->success();
    }

    /**
     * 关闭群聊
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws Exception
     */
    public function close(Request $request): JsonResponse
    {
        $this->validate($request, [
            'groupId' => 'required|int',
            'shopId'  => 'required|int',
        ]);

        $shopId   = $request->post('shopId');
        $groupId  = $request->post('groupId');
        $sendCard = (bool)$request->post('sendCard', true);

        ChatGroupSessionService::close($shopId, $groupId, !$sendCard);

        return $this->success();
    }
}
